package com.example.controller;

import com.example.model.MessageInfo;
import com.example.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;



@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;



    /**
     * 发布留言接口
     * @param messageInfo 留言板对象
     * @return
     */
    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo){
        //1.参数校验，检验参数是否为空
        System.out.println(StringUtils.hasLength(messageInfo.getFrom()));
        if(!StringUtils.hasLength(messageInfo.getFrom()) ||
                !StringUtils.hasLength(messageInfo.getTo()) ||
                !StringUtils.hasLength(messageInfo.getSay())){
            return false;
        }
        //2.存储数据到数据库中，调用逻辑层代码
        Integer result = messageService.addMessage(messageInfo);
        if(result > 0){
            return true;
        }
        return false;
    }

    /**
     * 获取留言接口
     * @return
     */
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.getMessageList();
    }
}
